package tms.controler;

import java.util.Date;

import javax.swing.JOptionPane;

import tms.model.Account;
import tms.model.Booking;
import tms.model.Destination;
import tms.model.Flight;
import tms.service.ServiceHandler;
import tms.view.ChangeView;
import tms.view.ContactInfoPanel;

public class ChangeController implements IController {

    private ChangeView view;
    private Booking currentTicket;

    public void cancelTicket() {
        if (currentTicket == null) {
            JOptionPane.showMessageDialog(view, "There is no ticket to cancel!");
        } else {
            Date departDate = currentTicket.getFlight().getDepartureDate();
            int diffInDays = (int) ((departDate.getTime() - (new Date()).getTime())
                     / (1000 * 60 * 60 * 24));
            if (diffInDays < 2) {
                JOptionPane.showMessageDialog(view, "It is impossible to " +
                        "cancel a ticket that the departure date is less than 2 days from now!");
            } else {
                int answer = JOptionPane.showConfirmDialog(view, "Do you want to cancel this ticket?",
                        "Cancel a ticket", JOptionPane.YES_NO_OPTION);
                if (answer == JOptionPane.YES_OPTION) {
                    if (ServiceHandler.deleteBooking(currentTicket)) {
                        JOptionPane.showMessageDialog(view, "You have cancel the ticket successfully!");
                        view.closeMe();
                    } else {
                        JOptionPane.showMessageDialog(view,
                                "Some errors occured while you are trying to cancel this ticket, please try again later!");
                    }
                }
            }
        }
    }

    public void getInfoFromTicketNum(String ticketNumber) {
        Booking ticket = ServiceHandler.getFirstBooking("TicketNumber= '" + ticketNumber + "'");
        if (ticket != null) {
            Flight flight = ticket.getFlight();

            Destination desDepFlight = flight.getDeparting();
            Destination desArrDepFlight = flight.getArriving();
            String depFlight = flight.getDepHour() + ":" + flight.getDepMinute() +
                    " (" + desDepFlight.getDestCode();
            depFlight += ") " + desDepFlight.getDestName();
            String arrDepFlight = "(" + desArrDepFlight.getDestCode();
            arrDepFlight += ")\n " + desArrDepFlight.getDestName();
            String depFlightDetail = flight.getFlightName();

            view.setDeparts(depFlight);
            view.setArrivers(arrDepFlight);
            view.setFlightDetail(depFlightDetail);

            Account passengerAccount = ticket.getBookingAccount();
            ContactInfoPanel pnlContactInfo = view.getContactInfoPanel();
            if (passengerAccount != null) {

                pnlContactInfo.setTitle(passengerAccount.getTitle());
                pnlContactInfo.setFname(passengerAccount.getFirstName());
                pnlContactInfo.setLname(passengerAccount.getLastName());
                pnlContactInfo.setPhoneNumber(passengerAccount.getPhone());
                pnlContactInfo.setEmail(passengerAccount.getEmail());
                pnlContactInfo.setCountry(passengerAccount.getCountry());
                pnlContactInfo.setAddress(passengerAccount.getAddress());
                pnlContactInfo.setDOB((passengerAccount.getBirthDate()));
            } else {
                pnlContactInfo.setFname(ticket.getPassengerName());
                pnlContactInfo.setLname(ticket.getPassengerName());
                pnlContactInfo.setPhoneNumber(ticket.getPassengerPhone());
                pnlContactInfo.setEmail(ticket.getPassengerEmail());
            }

            this.currentTicket = ticket;
        } else {
            JOptionPane.showMessageDialog(view,
                    "The system cannot find any ticket that maches your input number!");
        }

    }

    public boolean save() {

        ContactInfoPanel pnlContactInfo = view.getContactInfoPanel();
        String lname = pnlContactInfo.getLname();
        String fname = pnlContactInfo.getFname();
        String name = fname + lname;
        currentTicket.setPassengerName(name);
        currentTicket.setPassengerPhone(pnlContactInfo.getPhoneNumber());
        currentTicket.setPassengerEmail(pnlContactInfo.getEmail());
        if (!ServiceHandler.saveBooking(currentTicket)) {
            return false;
        }
        return true;
    }

    public void createView() {
        view = new ChangeView(this);
    }

    public Object getView() {
        if (view == null) {
            createView();
        }
        return view;
    }
}
